;:open2,8,15,"s:pack.cod":close2
;:open2,8,2,"pack.cod,p,w
;:sys36864
	*= 49152
	;.opt o2
pic1	=$2000
pic2	=$4000
tmp	=2
tmp2	=4
getpar	=$e1d4
chkkom	=$aefd
sa	=$b9
loadr	=$ffd5
pic3	=$6000
esc	=123
anz	=247
wert	=248
saver	=$ffd8
	jmp load
	jmp colin
	jmp pack
	jmp invert
	.byt 2
load	jsr chkkom
	jsr getpar
	ldx #<pic1
	ldy #>pic1
	lda #0
	sta sa
	jmp loadr
put	ldy #0
	sta (tmp2),y
	inc tmp2
	bne l1
	inc tmp2+1
l1	rts
get	ldy #0
	lda (tmp),y
	inc tmp
	bne l1
	inc tmp+1
	rts
back	lda tmp
	sec
	sbc #1
	sta tmp
	lda tmp+1
	sbc #0
	sta tmp+1
	rts
testend	lda tmp+1
	cmp #>pic2
	bne l5
	lda tmp
	cmp #<pic2
	bne l5
	sec
	rts
l5	clc
	rts
colin	ldx #4
	stx tmp+1
	ldy #0
	sty tmp
	lda #16
c	sta (tmp),y
	dey
	bne c
	inc tmp+1
	dex
	bne c
	rts
pack	lda #<pic1
	ldy #>pic1
	sta tmp
	sty tmp+1
	lda #<pic3
	ldy #>pic3
	sta tmp2
	sty tmp2+1
	sei
l4	lda #0
	sta anz
	jsr testend
	bcs l6
	inc anz
	jsr get
	sta wert
l3	jsr testend
	bcs l6
	jsr get
	cmp wert
	bne neu
	inc anz
	bne l3
	lda #esc
	jsr put
	lda #-1
	jsr put
	lda wert
	jsr put
	jsr back
	jmp l4
neu	jsr back
	ldx anz
	cpx #4
	bcs l9
	lda wert
	cmp #esc
	beq l9
la	jsr put
	dex
	bne la
	beq l4
l9	lda #esc
	jsr put
	lda anz
	jsr put
	lda wert
	jsr put
	jmp l4
l6	ldx anz
	beq l7
	cpx #4
	bcc lb
ld	lda #esc
	jsr put
	lda anz
	jsr put
	lda wert
	jsr put
	jmp l7
lb	lda wert
	cmp #esc
	beq ld
lc	jsr put
	dex
	bne lb
l7	lda #esc
	jsr put
	lda #0
	jsr put
	cli
	jsr chkkom
	jsr getpar
	ldx tmp2
	ldy tmp2+1
	lda #<pic3
	sta tmp
	lda #>pic3
	sta tmp+1
	lda #tmp
	sta sa
	jmp saver
invert	ldx #>pic1
	ldy #<pic1
	stx tmp+1
	sty tmp
i	lda (tmp),y
	eor #-1
	sta (tmp),y
	iny
	bne i
	inc tmp+1
	dex
	bne i
	rts
